package com.beerandjesus.blackjack.ui.awt;

import com.beerandjesus.blackjack.controller.GameController;
import com.beerandjesus.blackjack.controller.HandController;
import com.beerandjesus.blackjack.ui.CardDisplayer;
import com.beerandjesus.blackjack.ui.GameEventDisplayer;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

/**
 * User: reverend
 * Date: Jun 25, 2005
 * Time: 12:59:09 PM
 */
public class Blackjack extends Applet implements CardDisplayer, GameEventDisplayer {
    public static final String BUTTON_LABEL_HIT = "Hit";
    public static final String BUTTON_LABEL_STAY = "Stay";
    public static final String BUTTON_LABEL_START_GAME = "Start New Game";

    Button hit;
    Button stay;
    Button startGame;
    GameController controller;
    private HashMap canvases;

    public void init() {
        setLayout(new BorderLayout());

        canvases = new HashMap();
        controller = new GameController(this, this);

        add(newControlBar(), BorderLayout.SOUTH);
        add(newPlayArea(), BorderLayout.NORTH);


    }

    private Container newPlayArea() {
        Canvas playerCanvas = new CardCanvas(controller.getPlayerHand());
        playerCanvas.setSize(300, 150);
        canvases.put(HandController.IDENTIFIER, playerCanvas);

        Canvas dealerCanvas = new CardCanvas(controller.getDealerHand());
        dealerCanvas.setSize(300, 150);
        canvases.put(HandController.IDENTIFIER, dealerCanvas);

        Container cardBar = new Container();
        cardBar.setLayout(new FlowLayout());
        cardBar.add(playerCanvas);
        cardBar.add(dealerCanvas);
        return cardBar;
    }

    private Container newControlBar() {
        hit = new AbledObserverButton(BUTTON_LABEL_HIT, controller);
        hit.addActionListener(new HitActionListener());
        hit.setEnabled(false);

        stay = new AbledObserverButton(BUTTON_LABEL_STAY, controller);
        stay.addActionListener(new StayActionListener());
        stay.setEnabled(false);

        startGame = new Button(BUTTON_LABEL_START_GAME);
        startGame.addActionListener(new StartGameActionListener());

        Container controlBar = new Container();
        controlBar.setLayout(new FlowLayout());
        controlBar.add(startGame);
        controlBar.add(hit);
        controlBar.add(stay);
        return controlBar;
    }

    public GameController getController() {
        return controller;
    }

    public void displayCards(HandController hand) {
        Canvas canvas = getCanvasFor(hand);
        canvas.repaint();
    }

    private Canvas getCanvasFor(HandController hand) {
        return (Canvas)canvases.get(HandController.IDENTIFIER);
    }

    public void displayPlayerBlackjack() {
    }

    public void displayPlayerBust() {
    }

    public void displayDealerBlackjack() {
    }

    public void displayDealerBust() {
    }

    public void displayPush() {
    }

    public void displayPlayerWin() {
    }

    public void displayDealerWin() {
    }

    public void handlePlayerBlackjack() {
    }

    public void handlePlayerBust() {
    }

    public void handleDealerBlackjack() {
    }

    public void handleDealerBust() {
    }

    public void handlePush() {
    }

    public void handlePlayerWin() {
    }

    public void handleDealerWin() {
    }

    class StartGameActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            controller.dealNewGame();
        }
    }

    class HitActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            controller.dealToPlayer();
        }
    }

    class StayActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            controller.playOutDealer();
        }
    }
}
